﻿Imports System
Imports System.CodeDom
Imports System.ComponentModel.Design.Serialization
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.ComponentModel.Serialization
    Public Class ActivityCodeDomSerializer
        Inherits DependencyObjectCodeDomSerializer
        ' Methods
        Public Overrides Function Serialize(ByVal manager As IDesignerSerializationManager, ByVal obj As Object) As Object
            If (manager Is Nothing) Then
                Throw New ArgumentNullException("manager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            Dim activity As Activity = [TryCast](obj,Activity)
            If (activity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(Activity).FullName }), "obj")
            End If
            If Helpers.IsActivityLocked(activity) Then
                Return Nothing
            End If
            Dim statements As CodeStatementCollection = [TryCast](MyBase.Serialize(manager, activity),CodeStatementCollection)
            If (Not statements Is Nothing) Then
                Dim rootActivity As Activity = Helpers.GetRootActivity(activity)
                If (((rootActivity Is Nothing) OrElse (rootActivity.GetValue(ActivityCodeDomSerializer.MarkupFileNameProperty) Is Nothing)) OrElse (CInt(activity.GetValue(ActivityMarkupSerializer.StartLineProperty)) = -1)) Then
                    Return statements
                End If
                Dim statement As CodeStatement
                For Each statement In statements
                    If Not TypeOf statement Is CodeCommentStatement Then
                        statement.LinePragma = New CodeLinePragma(CStr(rootActivity.GetValue(ActivityCodeDomSerializer.MarkupFileNameProperty)), Math.Max(CInt(activity.GetValue(ActivityMarkupSerializer.StartLineProperty)), 1))
                    End If
                Next
            End If
            Return statements
        End Function


        ' Fields
        Public Shared ReadOnly MarkupFileNameProperty As DependencyProperty = DependencyProperty.RegisterAttached("MarkupFileName", GetType(String), GetType(ActivityCodeDomSerializer), New PropertyMetadata(Nothing, New Attribute() { New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden) }))
    End Class
End Namespace

